package cn.topkinson.es.etl.service;

import cn.topkinson.es.etl.entity.es.IndexEnum;
import cn.topkinson.es.etl.entity.es.SearchDto;
import cn.topkinson.es.etl.entity.es.SearchResult;
import cn.topkinson.es.etl.entity.es.UpdateDto;
import cn.topkinson.es.etl.entity.po.Position;
import cn.topkinson.es.etl.entity.po.PositionExample;
import cn.topkinson.es.etl.mapper.PositionMapper;
import cn.topkinson.es.etl.utils.EsHelper;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author huangzihao
 * @Date 2021/1/22 20:42
 */
@Service
public class MainService {

    @Autowired
    private EsHelper esHelper;

    @Autowired
    private PositionMapper positionMapper;

    private Map<String, Object> convertData(Position po){
        Map<String, Object> data = new HashMap<>();
        data.put("companyName",po.getCompanyName());
        data.put("id",po.getId());
        data.put("positionAdvantage",po.getPositionAdvantage());
        data.put("companyId",po.getCompanyId());
        data.put("positionName",po.getPositionName());
        data.put("salary",po.getSalary());
        data.put("salaryMin",po.getSalaryMin());
        data.put("salaryMax",po.getSalaryMax());
        data.put("salaryMonth",po.getSalaryMonth());
        data.put("education",po.getEducation());
        data.put("workYear",po.getWorkYear());
        data.put("jobNature",po.getJobNature());
        data.put("createTime",po.getCreateTime());
        data.put("email",po.getEmail());
        data.put("publishTime",po.getPublishTime());
        data.put("isEnable",po.getIsEnable());
        data.put("isIndex",po.getIsIndex());
        data.put("city",po.getCity());
        data.put("orderby",po.getOrderby());
        data.put("isAdvice",po.getIsAdvice());
        data.put("showorder",po.getShoworder());
        data.put("publishUserId",po.getPublishUserId());
        data.put("workAddress",po.getWorkAddress());
        data.put("generateTime",po.getGenerateTime());
        data.put("bornTime",po.getBornTime());
        data.put("isReward",po.getIsReward());
        data.put("rewardMoney",po.getRewardMoney());
        data.put("isExpired",po.getIsExpired());
        data.put("positionDetailPV",po.getPositionDetailPV());
        data.put("offlineTime",po.getOfflineTime());
        data.put("positionDetailPV_cnbeta",po.getPositionDetailPV_cnbeta());
        data.put("adviceTime",po.getPositionDetailPV());
        data.put("comeFrom",po.getPositionDetailPV());
        data.put("receivedResumeCount",po.getReceivedResumeCount());
        data.put("refuseResumeCount",po.getRefuseResumeCount());
        data.put("markCanInterviewCount",po.getMarkCanInterviewCount());
        data.put("haveNoticeInterCount",po.getHaveNoticeInterCount());
        data.put("isForbidden",po.getIsForbidden());
        data.put("reason",po.getReason());
        data.put("verifyTime",po.getVerifyTime());
        data.put("adWord",po.getAdWord());
        data.put("adRankAndTime",po.getAdRankAndTime());
        data.put("adTimes",po.getAdTimes());
        data.put("adStartTime",po.getAdStartTime());
        data.put("adEndTime",po.getAdStartTime());
        data.put("adBeforeDetailPV",po.getAdBeforeDetailPV());
        data.put("adAfterDetailPV",po.getAdAfterDetailPV());
        data.put("adBeforeReceivedCount",po.getAdBeforeReceivedCount());
        data.put("adAfterReceivedCount",po.getAdAfterReceivedCount());
        data.put("adjustScore",po.getAdjustScore());
        data.put("weightStartTime",po.getWeightStartTime());
        data.put("weightEndTime",po.getWeightEndTime());
        data.put("isForward",po.getIsForward());
        data.put("forwardEmail",po.getForwardEmail());
        data.put("isSchoolJob",po.getIsSchoolJob());
        data.put("type",po.getType());
        data.put("prolong_offline_time",po.getProlong_offline_time());
        data.put("chargeField",po.getChargeField());

        return data;
    }

    public void etl(){
        PositionExample positionExample = new PositionExample();
        positionExample.createCriteria().andIdIsNotNull();
        List<Position> positions = positionMapper.selectByExample(positionExample);
        positions.forEach(po->{
            try {
                esHelper.upsert(new UpdateDto(IndexEnum.LAGOU_POSITION,po.getId()+"",convertData(po)));
            } catch (IOException e) {
                e.printStackTrace();
            }
        });

    }

    public List<HashMap> search_position(String name) {
        BoolQueryBuilder queryBuilder = new BoolQueryBuilder();
        queryBuilder.must(QueryBuilders.termQuery("positionName", name));

        SearchDto searchDto = new SearchDto();
        searchDto.setIndexEnum(IndexEnum.LAGOU_POSITION);
        searchDto.setIncludes(new String[]{"education", "jobNature", "companyName","salary","city","workYear","positionName","positionAdvantage"});
        searchDto.setFrom(0);
        searchDto.setSize(5);
        searchDto.setQueryBuilder(queryBuilder);
        SearchResult<HashMap> result = esHelper.pageHits(searchDto, HashMap.class);
        List<HashMap> list = result.getList();
        if (list.size()<5){
            int size = list.size();
            BoolQueryBuilder queryBuilder_ext = new BoolQueryBuilder();
            queryBuilder_ext.should(QueryBuilders.matchQuery("positionAdvantage","美女多"));
            queryBuilder_ext.should(QueryBuilders.matchQuery("positionAdvantage","员工福利好"));

            SearchDto searchDto_ext = new SearchDto();
            searchDto_ext.setIndexEnum(IndexEnum.LAGOU_POSITION);
            searchDto_ext.setFrom(0);
            searchDto_ext.setSize(5-size);
            searchDto_ext.setQueryBuilder(queryBuilder_ext);
            searchDto_ext.setIncludes(new String[]{"education", "jobNature", "companyName","salary","city","workYear","positionName","positionAdvantage"});
            SearchResult<HashMap> result_ext = esHelper.pageHits(searchDto_ext, HashMap.class);
            list.addAll(result_ext.getList());
        }
        return list;
    }
}
